package com.lfc.myframe

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.multidex.MultiDex
import androidx.multidex.MultiDexApplication
import com.lfc.myframe.service.CrashManager
import com.lfc.myframe.share.Const
import com.lfc.myframe.utils.LgU
import com.lfc.myframe.utils.network.NetStateReceiver
import com.tamsiree.rxkit.RxDeviceTool
import com.tamsiree.rxkit.RxTool
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.bugly.crashreport.CrashReport.UserStrategy
import kotlin.properties.Delegates


/**
 * Created by LFC
on 2020/5/13.
 */
//@HiltAndroidApp
class MyApp : MultiDexApplication() {

    companion object {
        val TAG = "wan_android"

        var context: Context by Delegates.notNull()
            private set

        lateinit var instance: Application


    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        context = applicationContext
        initConfig()
//        DisplayManager.init(this)
        registerActivityLifecycleCallbacks(mActivityLifecycleCallbacks)
        initBugly()
    }

    private fun initBugly() {

        val strategy = UserStrategy(this)
        strategy.appChannel = "myChannel";  //设置渠道
        strategy.appVersion = RxDeviceTool.getAppVersionName(this);      //App的版本
        strategy.appPackageName = RxDeviceTool.appPackageName;  //App的包名
        strategy.appReportDelay = 10000;   //  延时 初始化 改为10s
        CrashReport.initCrashReport(this, "cd976863b2", Const.ISDebug, strategy);

    }

    private fun initConfig() {
        RxTool.init(this)
        CrashManager.install(this) //异常 捕获 初始化
        NetStateReceiver.registerNetworkStateReceiver(this)

        if (BuildConfig.ISRelease) {
//        if (true) {
            Const.ISDebug = false
            LgU.LEVEL = LgU.DEBUG
        } else {
            Const.ISDebug = true
            LgU.LEVEL = LgU.VERBOSE
        }

        /////TODO  最简单解决  ----7.0----先测试----（已解决）///方法可行
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            val builder = StrictMode.VmPolicy.Builder()
//            StrictMode.setVmPolicy(builder.build())
//        }


    }

    override fun attachBaseContext(base: Context) {
//        super.attachBaseContext(MultiLanguageUtils.attachBaseContext(base))
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    private val mActivityLifecycleCallbacks = object : ActivityLifecycleCallbacks {
        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            Log.d(TAG, "onCreated: " + activity.componentName.className)
        }

        override fun onActivityStarted(activity: Activity) {
            Log.d(TAG, "onStart: " + activity.componentName.className)
        }

        override fun onActivityResumed(activity: Activity) {

        }

        override fun onActivityPaused(activity: Activity) {

        }

        override fun onActivityStopped(activity: Activity) {

        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

        }

        override fun onActivityDestroyed(activity: Activity) {
            Log.d(TAG, "onDestroy: " + activity.componentName.className)
        }
    }
}